-- BABYLON_NEW_LA_BY_NWFLOWER
-- Author: 千川白浪
-- DateCreated: 2025-6-15 22:23:02
--------------------------------------------------------------
--||=======================include========================||--
include "HD_StateUtils"  
Utils = ExposedMembers.DLHD.Utils;

print('HD_BBL Script Loading')
local BUILDING_TRAIT_LEADER_NEBUCHADNEZZAR_II_NEW_INDEX = GameInfo.Buildings['BUILDING_TRAIT_LEADER_NEBUCHADNEZZAR_II_NEW'].Index

function NW_BABYLON_PRODUCTION_FOR_WONDER_OR_WALLS(playerId, cityId, productionId, objectId)
    -- 检查玩家是否拥有领袖特质 略
    if not Utils.LeaderHasTrait(playerId, 'TRAIT_LEADER_NEBUCHADNEZZAR_II_NEW') then return end

    -- 检查参数合法性
    local player = Players[playerId]
    if not player then return end
    local pCity = player:GetCities():FindID(cityId)
    if not pCity then return end
    local buildQueue = pCity:GetBuildQueue()
    if not buildQueue then return end

    -- 获取当前生产项目
    local currentProduction = buildQueue:CurrentlyBuilding()
    if not currentProduction then return end

    -- 检查是否为奇观或城墙
    local buildingInfo = GameInfo.Buildings[currentProduction]
    if buildingInfo and buildingInfo.IsWonder then
        print('奇观')
        NW_BABYLON_GIVE_BUILDING(pCity)
    elseif buildingInfo and GameInfo.HD_BABYLON_DEFENSE_BUILDINGS[buildingInfo.BuildingType] then
        print('城墙')
        NW_BABYLON_GIVE_BUILDING(pCity)
    else
        print('其他')
        NW_BABYLON_REMOVE_BUILDING(pCity)
    end
end

-- 基于虚拟建筑
function NW_BABYLON_GIVE_BUILDING(pCity)
    if not (pCity:GetBuildings():HasBuilding(BUILDING_TRAIT_LEADER_NEBUCHADNEZZAR_II_NEW_INDEX)) then
        pCity:GetBuildQueue():CreateBuilding(BUILDING_TRAIT_LEADER_NEBUCHADNEZZAR_II_NEW_INDEX);
    end
end

-- 删除虚拟建筑
function NW_BABYLON_REMOVE_BUILDING(pCity)
    if pCity:GetBuildings():HasBuilding(BUILDING_TRAIT_LEADER_NEBUCHADNEZZAR_II_NEW_INDEX) then
        pCity:GetBuildings():RemoveBuilding(BUILDING_TRAIT_LEADER_NEBUCHADNEZZAR_II_NEW_INDEX);
    end
end

-- =====================================================================
-- INIT
-- =====================================================================
function initialize()
    Events.CityProductionChanged.Add(NW_BABYLON_PRODUCTION_FOR_WONDER_OR_WALLS)
    print('HD_BBL Script Success')
end

Events.LoadGameViewStateDone.Add(initialize)